<template>
	<view>
		<div class="loading" v-show="loading">
			<van-skeleton title avatar row="15">
			</van-skeleton>
		</div>
		<van-cell-group title="  ">
			<van-cell v-show='!showDefault' clickable size="large" custom-class="login" @click="showMyInfo">
				<view slot="title">
					<van-row>
						<van-col span="6" offset="1">
							<open-data type="userAvatarUrl" />
						</van-col>
						<van-col span="16" offset="1">
							<open-data type="userNickName" lang="zh_CN" class="text" />
						</van-col>
					</van-row>
				</view>
			</van-cell>
			<van-cell v-show="showDefault" clickable size="large" custom-class="login" @click="login"
				@tap="getUserInfoByOpenid(e)">
				<view slot="title">
					<van-row>
						<van-col span="6" offset="1">
							<van-image fit="cover" use-loading-slot width="66" height="66" src='/static/img/login.png'>
							</van-image>
						</van-col>
						<van-col span="16" offset="1">
							<div class="text" v-show="showDefault">点击登录</div>
						</van-col>
					</van-row>
				</view>
			</van-cell>
		</van-cell-group>
		<div class="settings">
			<van-cell-group>
				<!-- <van-cell v-show='!showDefault' title="我的小纸条" clickable link-type="navigateTo" url="/pages/notes/notes" 
					is-link value="查看私信" /> -->
				<van-cell v-show='!showDefault&&isSDU' title="导入课表" clickable is-link link-type="navigateTo"
					url="/pages/importClassTable/importClassTable" value="点击即可导入课表" />
				<van-cell v-show='!showDefault' title="设置" clickable is-link link-type="navigateTo"
					url="/pages/myInfo/myInfo" value="完善或更改个人信息" />
				<van-cell v-show='!showDefault' title="清空课表" clickable is-link @click="popConfirmDelete"
					value="点击即可清空课表" />
				<van-dialog id="van-dialog" confirm-button-color="#006600" />
				<!-- <van-cell v-show='!showDefault' title="找同学" clickable is-link @click="popChooseClassmates"
					value="来找同学聊天" /> -->
				<van-dialog id="van-dialog" confirm-button-color="#006600" />
			</van-cell-group>
			<van-empty v-show='showDefault' description="登录后体验更多功能~" />
		</div>
		<van-notify id="van-notify" />
	</view>
</template>
<script>
	import Notify from '../../wxcomponents/vant/dist/notify/notify';
	import Dialog from '../../wxcomponents/vant/dist/dialog/dialog';
	export default {
		data() {
			return {
				code: "",
				openid: "",
				token: "",
				showDefault: true,
				loading: false,
				isSDU: false
			}
		},
		beforeCreate() {
			this.loading = true
			wx.showLoading({
				title:"加载中"
			})
		},

		onShow() {
			if (this.showDefault === false) {
				this.querySchool()
			}
		},
		 mounted() {
			let token = ""
			token = wx.getStorageSync('token')
			if (token !== "") {
				 this.autoLogin()
			}
		
			wx.hideLoading()
			this.loading = false
		},
		methods: {
	
	
			autoLogin(){
				let self = this
				wx.login({
					success(res) {
						wx.request({
							url: getApp().globalData.baseURL + "/login",
							data: {
								"code": res.code
							},
							success(res) {
								if (res.data.code === 1) {
									self.openid = res.data.data.openid
									self.token = res.data.data.token
									wx.setStorageSync('openid', self.openid)
									wx.setStorageSync('token', self.token)
									self.querySchool()
									Notify({
										type: 'success',
										message: '登陆成功'
									});
									self.showDefault = false
								} else {
									Notify({
										type: 'danger',
										message: '获取用户信息失败,请重试'
									});
								}
				
							},
							fail(err) {
								Notify({
									type: 'danger',
									message: '连接服务器失败'
								});
							}
						})
					},
					fail(err) {
						Notify({
							type: 'danger',
							message: '获取用户登录态失败'
						});
					}
				})
			},
			popChooseClassmates() {
				Dialog.confirm({
						message: '请选择您要找的同学',
						confirmButtonText: "找同班",
						cancelButtonText: "找同级"
					})
					.then(() => {
						uni.navigateTo({
							url: "../ClassmatesList/ClassmatesList"
						})

					})
					.catch(() => {
					})
			},
			popConfirmDelete() {
				Dialog.confirm({
						message: '确定要清空课表吗?',
					})
					.then(() => {
						uni.request({
							url: getApp().globalData.baseURL + "/course/deleteAllCourseByOpenid",
							data: {
								"openid": wx.getStorageSync('openid')
							},
							header: {
								"token": wx.getStorageSync('token')
							},
							success: (res) => {
								Notify({
									type: 'success',
									message: '已清空所有课程'
								});
								console.log(res)
							},
							fail: (err) => {
								Notify({
									type: 'danger',
									message: '删除失败'
								});
							}
						})
					})
					.catch(() => {

					});
			},
			querySchool() {
				let self = this
				uni.request({
					url: getApp().globalData.baseURL + "/user/getUser",
					data: {
						"openid": wx.getStorageSync('openid')
					},
					header: {
						"token": wx.getStorageSync('token')
					},
					success: (res) => {

						if (res.data.code == -1) {
							self.isSDU = false
						} else {
							if (res.data.data.school === "山东大学") {
								self.isSDU = true
							} else {
								self.isSDU = false
							}
						}
					},
					fail: (err) => {
						Notify({
							type: 'danger',
							message: '获取用户信息失败'
						});
					}
				})
			},
			showMyInfo() {
				uni.navigateTo({
					url: "../showMyInfo/showMyInfo"
				})
			},
			// 添加用户昵称等
			addAUserInfo(res) {
				uni.request({
					url: getApp().globalData.baseURL + "/userInf/addAUserInf",
					data: {
						"openid": wx.getStorageSync('openid'),
						"nickname": res.userInfo.nickName,
						"headImg": res.userInfo.avatarUrl
					},
					header: {
						"token": wx.getStorageSync('token')
					},
					success: (res) => {
						console.log(res)
					},
					fail: (err) => {
						Notify({
							type: 'danger',
							message: '添加用户信息失败'
						});
					}
				})
			},

			getUserInfoByOpenid(e) {
				let self = this
				// 查询用户信息
				uni.request({
					url: getApp().globalData.baseURL + "/userInf/getUserInfByOpenid",
					data: {
						"openid": wx.getStorageSync('openid')
					},
					header: {
						"token": wx.getStorageSync('token')
					},
					success: (res) => {
						if (res.data.data === null) {
							wx.getUserProfile({
								desc: "用于完善用户资料",
								success: (res) => {
									self.addAUserInfo(res)
								},
								fail: (err) => {
									Notify({
										type: 'danger',
										message: '查询用户信息失败'
									});
								}
							})
						}

					},
					fail: (err) => {
						console.log(err)
					}
				})

			},

			login() {
				let self = this
				wx.login({
					success(res) {
						wx.request({
							url: getApp().globalData.baseURL + "/login",
							data: {
								"code": res.code
							},
							success(res) {
								if (res.data.code === 1) {
									self.openid = res.data.data.openid
									self.token = res.data.data.token
									wx.setStorageSync('openid', self.openid)
									wx.setStorageSync('token', self.token)
									Notify({
										type: 'success',
										message: '登陆成功'
									});
									self.showDefault = false
								} else {
									Notify({
										type: 'danger',
										message: '获取用户信息失败,请重试'
									});
								}

							},
							fail(err) {
								Notify({
									type: 'danger',
									message: '连接服务器失败'
								});
							}
						})
					},
					fail(err) {
						Notify({
							type: 'danger',
							message: '获取用户登录态失败'
						});
					}
				})
			}
		},
	}
</script>

<style scoped>
	.text {
		color: #808080;
		position: absolute;
		top: 50%;
		transform: translateY(-50%);
	}

	.custom-icon {
		margin-left: 10rpx;
	}

	.settings {
		margin-top: 50rpx;
	}

	.loading {
		margin-top: 30rpx;
	}
</style>
